//author:kakake
class tssyncdataController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        dbConnBoot: TSLobsterAPI.Program.GetDbConn("LobsterBoot"),
        WorkId: 1
    }

    public service: tssyncdataService;
    public dao: tssyncdataDao;

    constructor() {
        this.data.WorkId = TSLobsterAPI.Program.GetWorkId();

        this.service = new tssyncdataService(this.data.WorkId);
        this.dao = new tssyncdataDao(this.data.WorkId);

    }


    /**同步机构数据 */
    startsync(currworkid) {
        var log1, log2, log3, log4;
        var ssowork = this.dao.getSSOWork(currworkid);
        if (!ssowork) throw '无效机构';
        var ssouserid = ssowork.CreatedUserId;

        //同步机构
        if (this.dao.existHISWork(currworkid) == false) {
            // TSLobsterAPI.Utils.TrackLog("ssowork", ssowork);
            this.dao.insertHISWork(ssowork);
            log1 = '机构信息同步成功';
        } else {
            log1 = "机构信息已存在不需要同步";
        }

        //同步机构管理员
        if (this.dao.existSSOUser(ssouserid, currworkid) == false) {
            //未绑定HIS账号
            var ssouseremp = this.dao.getSSOUser(ssouserid, currworkid);
            //TSLobsterAPI.Utils.TrackLog('ssouseremp',ssouseremp);
            if (this.dao.existHISUser(currworkid, ssouseremp.user.UserCode, ssouseremp.emp.Name, ssouseremp.emp.Telephone) == false) {
                if (this.dao.existHISEmp(currworkid, ssouseremp.emp.Name, ssouseremp.emp.Telephone) == false) {
                    var empid = this.dao.insertHISEmp(currworkid, ssouseremp.emp);
                    this.dao.insertHISUser(currworkid, ssouseremp, empid);
                    //empid回写到SSO
                    this.dao.updateSSOhisempid(currworkid, ssouseremp.emp.EmpId, empid);
                } else {
                    var empid = this.dao.getHISEmpid(currworkid, ssouseremp.emp.Name, ssouseremp.emp.Telephone);
                    //HIS未有SSO用户，则插入
                    this.dao.insertHISUser(currworkid, ssouseremp, empid);
                }
            }

            //HIS已有SSO用户，绑定到SSO
            var hiscode = ssouseremp.user.UserCode;//SSO用户名就是HIS用户名
            this.dao.bindSSOUser(ssouserid, currworkid, hiscode);

            log2 = '机构管理员同步成功';
        } else {
            log2 = '机构管理员已绑定HIS账号';
        }

        //同步科室和科室分类
        log3 = '科室信息同步成功';
        if (this.dao.existHISDeptlayer(currworkid) == false) {
            var ssodata = this.dao.getSSODeptlayer(currworkid);
            this.dao.insertHISDeptlayer(currworkid, ssodata.ssobranch, ssodata.ssodeptlayerlist);
            log3 = log3 + ',科室分类同步成功';
        } else {
            log3 = log3 + ',科室分类无需同步';
        }

        var succcount3 = 0;
        var ssodeptlist = this.dao.getSSODept(currworkid);
        ssodeptlist.forEach(ssodept => {
            if (this.dao.existHISDept(currworkid, ssodept.Name) == false) {
                var deptid = this.dao.insertHISDept(currworkid, ssodept);
                this.dao.updateSSOhisdeptid(currworkid, ssodept.DeptId, deptid);
                succcount3 = succcount3 + 1;
            }
        })
        log3 = log3 + (',需要同步的科室数量：' + ssodeptlist.length) + (',成功同步的科室数量：' + succcount3);

        //同步机构人员
        log4 = '人员信息同步成功';
        var ssoemplist = this.dao.getSSOEmpList(currworkid);
        var succcount4 = 0;
        ssoemplist.forEach(item => {
            //TSLobsterAPI.Utils.TrackLog('item', item);
            if (this.dao.existHISEmp(currworkid, item.Name, item.Telephone) == false) {
                var empid = this.dao.insertHISEmp(currworkid, item);
                //empid回写到SSO
                this.dao.updateSSOhisempid(currworkid, item.EmpId, empid);
                succcount4 = succcount4 + 1;

                //更新人员所属科室
                var empdept = this.dao.getSSOEmpDept(currworkid, item.EmpId);
                empdept.forEach(dept => {
                    var deptid = this.dao.getHISDeptid(currworkid, dept.Name);
                    this.dao.insertHISEmpDept(currworkid, empid, deptid, dept.DefaultFlag);
                })
            }
        })

        log4 = log4 + (',需要同步的人员数量：' + ssoemplist.length) + (',成功同步的人员数量：' + succcount4);
        return { log1: log1, log2: log2, log3: log3, log4: log4 }
    }

    /**同步创建用户 */
    usersync(ssouserid, currworkid) {
        if (this.dao.existHISWork(currworkid) == false) {
            throw '当前机构在HIS系统中不存在';
        }
        if (this.dao.existSSOUser(ssouserid, currworkid) == false) {
            //未绑定HIS账号
            var ssouseremp = this.dao.getSSOUser(ssouserid, currworkid);
            //TSLobsterAPI.Utils.TrackLog('ssouseremp',ssouseremp);
            if (this.dao.existHISUser(currworkid, ssouseremp.user.UserCode, ssouseremp.emp.Name, ssouseremp.emp.Telephone) == false) {
                if (this.dao.existHISEmp(currworkid, ssouseremp.emp.Name, ssouseremp.emp.Telephone) == false) {
                    var empid = this.dao.insertHISEmp(currworkid, ssouseremp.emp);
                    this.dao.insertHISUser(currworkid, ssouseremp, empid);
                    //empid回写到SSO
                    this.dao.updateSSOhisempid(currworkid, ssouseremp.emp.EmpId, empid);
                } else {
                    var empid = this.dao.getHISEmpid(currworkid, ssouseremp.emp.Name, ssouseremp.emp.Telephone);
                    //HIS未有SSO用户，则插入
                    this.dao.insertHISUser(currworkid, ssouseremp, empid);
                }
            }

            //HIS已有SSO用户，绑定到SSO
            var hiscode = ssouseremp.user.UserCode;//SSO用户名就是HIS用户名
            this.dao.bindSSOUser(ssouserid, currworkid, hiscode);
        }
    }
}